package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Area;
import com.ruoyi.system.service.IAreaService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 地区Controller
 *
 * @author gzhuangwei
 * @date 2022-02-27
 */
@RestController
@RequestMapping("/business/area")
public class AreaController extends BaseController
{
    @Autowired
    private IAreaService areaService;

    /**
     * 查询地区列表
     */
    @PreAuthorize("@ss.hasPermi('business:area:list')")
    @GetMapping("/list")
    public AjaxResult list(Area area)
    {
        List<Area> list = areaService.selectAreaList(area);
        return AjaxResult.success(list);
    }

    /**
     * 导出地区列表
     */
    @PreAuthorize("@ss.hasPermi('business:area:export')")
    @Log(title = "地区", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Area area)
    {
        List<Area> list = areaService.selectAreaList(area);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        return util.exportExcel(list, "地区数据");
    }

    /**
     * 获取地区详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:area:query')")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") Long areaId)
    {
        return AjaxResult.success(areaService.selectAreaById(areaId));
    }

    /**
     * 新增地区
     */
    @PreAuthorize("@ss.hasPermi('business:area:add')")
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Area area)
    {
        // 创建日期Date对象 - 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        area.setAreaCreateTime(timeStamp);
        // 创建人帐户ID
        Long createId = SecurityUtils.getLoginUser().getUser().getUserId();
        area.setAreaCreateId(createId);
        // 得到创建人帐户用户名
        String createBy = SecurityUtils.getLoginUser().getUser().getUserName();
        area.setAreaCreateBy(createBy);
        // 新增流向来源，设置审核状态为：待提交
        area.setAreaState("initial");
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改地区
     */
    @PreAuthorize("@ss.hasPermi('business:area:edit')")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Area area)
    {
        // 修改日期 - 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        area.setAreaUpdateTime(timeStamp);
        // 修改人帐户ID
        Long updateId = SecurityUtils.getLoginUser().getUser().getUserId();
        // 得到修改人帐户ID
        String updateBy = SecurityUtils.getLoginUser().getUser().getUserName();
        area.setAreaUpdateId(updateId);
        area.setAreaUpdateBy(updateBy);
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 提交地区供审批
     */
    @PreAuthorize("@ss.hasPermi('business:area:submit')")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @DeleteMapping("/submit/{areaIds}")
    public AjaxResult submit(@PathVariable Long[] areaIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例，将状态设置成待审批
        Area area = new Area();
        area.setAreaState("approval");
        for(Long areaId : areaIds){
            area.setAreaId(areaId);
            result = areaService.updateArea(area);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 审批通过地区
     */
    @PreAuthorize("@ss.hasPermi('business:area:pass')")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @DeleteMapping("/pass/{areaIds}")
    public AjaxResult pass(@PathVariable Long[] areaIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例
        Area area = new Area();
        // 审批人帐户ID
        Long approveId = SecurityUtils.getLoginUser().getUser().getUserId();
        area.setAreaApproveId(approveId);
        // 审批时间 - 创建Date对象，并得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        area.setAreaApproveTime(timeStamp);
        // 实例状态
        area.setAreaState("definite");
        // 变更对应的实例审批状态
        for(Long areaId : areaIds){
            area.setAreaId(areaId);
            result = areaService.updateArea(area);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 反审批退回地区
     */
    @PreAuthorize("@ss.hasPermi('business:area:back')")
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @DeleteMapping("/back/{areaIds}")
    public AjaxResult back(@PathVariable Long[] areaIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例，将状态设置成待审批
        Area area = new Area();
        area.setAreaState("approvalback");
        Long approveId = SecurityUtils.getLoginUser().getUser().getUserId();
        area.setAreaApproveId(approveId);
        // 创建Date对象，并得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        area.setAreaApproveTime(timeStamp);
        for(Long areaId : areaIds){
            area.setAreaId(areaId);
            result = areaService.updateArea(area);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 删除地区
     */
    @PreAuthorize("@ss.hasPermi('business:area:remove')")
    @Log(title = "地区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{areaIds}")
    public AjaxResult remove(@PathVariable Long[] areaIds)
    {
        return toAjax(areaService.deleteAreaByIds(areaIds));
    }

    /**
     * 返回所有地区
     */
    @GetMapping("/all")
    public AjaxResult getAreas()
    {
        Area area = new Area();
        List<Area> list = areaService.selectAreaList(area);
        if (StringUtils.isNull(list))
        {
            list = new ArrayList<>();
        }
        return AjaxResult.success(list);
    }
}